/**
 * Created by 33183 on 2017/12/7.
 */
//domain = "219.140.167.92";
var threeDayForecastUrl = "../api/ShortForecast/City3DForecast";

//test
//将url参数转换成hashmap对象
function paramToHashMap(param) {
 var tmpArr = param.split("&");
 var hashMap = new Object();
 for ( var i = 0; i < tmpArr.length; i++) {
 var para = tmpArr[i];
 if (para.indexOf("=") != -1) {
 var kv = para.split("=");
 hashMap[kv[0]] = kv[1];
 }
 }
 return hashMap;
 }

 var params = paramToHashMap(document.location.search.split("?")[1]);
 var stationNo = decodeURIComponent(params["stationNo"]);
 var CountyName = decodeURIComponent(params["CountyName"]);
 var h24WeatherCode = decodeURIComponent(params["h24WeatherCode"]);
 var h48WeatherCode = decodeURIComponent(params["h48WeatherCode"]);
 var h72WeatherCode = decodeURIComponent(params["h72WeatherCode"]);
 var h96WeatherCode = decodeURIComponent(params["h96WeatherCode"]);
 var h120WeatherCode = decodeURIComponent(params["h120WeatherCode"]);
 var h144WeatherCode = decodeURIComponent(params["h144WeatherCode"]);
 var h168WeatherCode = decodeURIComponent(params["h168WeatherCode"]);
Highcharts.setOptions({
    lang : {
        months : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月',
            '九月', '十月', '十一月', '十二月' ],
        weekdays : [ '周日', '周一', '周二', '周三', '周四', '周五', '周六' ],
        shortMonths:['01','02','03','04','05','06','07','08','09','10','11','12'],
        downloadPNG : "下载 PNG 图片",
        downloadJPEG : "下载 JPEG 图片",
        downloadPDF : "下载 PDF 文档",
        downloadSVG : "下载SVG矢量图",
        exportButtonTitle : "输出图片",
        loading : "稍等...",
        printChart : "打印图表"
    }
});
function tooltipFormatter(tooltip) {
    var index = tooltip.points[0].point.index,
        ret = '<small>'
            + Highcharts.dateFormat('%A, %m月%e日', tooltip.x)
            + '</small><br>';
    ret += '<table>';
    Highcharts.each(tooltip.points,function(point) {
        var series = point.series;
        ret += '<tr><td><span style="color:' + series.color + '">\u25CF</span> '
            + series.name
            + ': </td><td style="white-space:nowrap">'
            + Highcharts.pick(point.point.value,
                point.y)
            + (series.options.tooltip
            && series.options.tooltip.valueSuffix ? series.options.tooltip.valueSuffix
                : "");
        ret += '</td></tr>';
    });
    ret += '</table>';
    return ret;
};
var title = '';
var dayData = [];
var nightData = [];
$(function() {
    title = CountyName + "7天天气预报";
    document.title = title;
    //---------------------

    initChart();

     $.ajax({url:"../metCityForecast/getChart",
     type: "post",
     data: { stationNo: stationNo},
     dataType: "json",
     //jsonpCallback: "setChartData",//callback的function名称
     success: function (data) {
         console.log(data)
         var time = new Date(parseInt(data[0].PublishTime)).Format("yyyy-MM-dd hh:mm")
         $('#publishTab').html("发布时间："+time);
     //debugger;
         setChartData(data);
     },
     error: function (data) {
     //alert("获取数据失败！");
     }
     });



});

function setChartData(data) {
    //var data[0] = JSON.stringify(data);
    //var marker = {symbol:""};
    var oneDay = {}, twoDay = {}, threeDay = {}, fourDay = {}, fiveDay = {}, sixDay = {},sevenDay = {},
        oneNight = {}, twoNight = {}, threeNight = {},fourNight = {}, fiveNight = {}, sixNight = {}, sevenNight = {};
    var time = new Date(parseInt(data[0].ForecastStartTime)).Format("yyyy-MM-dd hh:mm");

    var times = time.split(" ")[0].split("-");
    oneDay.x = oneNight.x = Date.UTC(times[0], times[1]-1, times[2]);
    twoDay.x = twoNight.x = Date.UTC(times[0], times[1]-1, times[2]) + 86400000;
    threeDay.x = threeNight.x = Date.UTC(times[0], times[1]-1, times[2]) + 86400000 * 2;
    fourDay.x = fourNight.x = Date.UTC(times[0], times[1]-1, times[2]) + 86400000 * 3;
    fiveDay.x = fiveNight.x = Date.UTC(times[0], times[1]-1, times[2]) + 86400000 * 4;
    sixDay.x = sixNight.x = Date.UTC(times[0], times[1]-1, times[2]) + 86400000 * 5;
    sevenDay.x = sevenNight.x = Date.UTC(times[0], times[1]-1, times[2]) + 86400000 * 6;
    oneDay.y = Number(data[0].h24HTemp);
    twoDay.y = Number(data[0].h48HTemp);
    threeDay.y =Number(data[0].h72HTemp);
    fourDay.y = Number(data[0].h96HTemp);
    fiveDay.y = Number(data[0].h120HTemp);
    sixDay.y =Number(data[0].h144HTemp);
    sevenDay.y =Number(data[0].h168HTemp);
    oneNight.y = Number(data[0].h24LTemp);
    twoNight.y = Number(data[0].h48LTemp);
    threeNight.y = Number(data[0].h72LTemp);
    fourNight.y = Number(data[0].h96LTemp);
    fiveNight.y = Number(data[0].h120LTemp);
    sixNight.y = Number(data[0].h144LTemp)
    sevenNight.y = Number(data[0].h168LTemp)
    //oneDay.marker={symbol:getSymbolByWeatherCode(data[0].h12WeatherCode, 0) };
    //twoDay.marker={symbol:getSymbolByWeatherCode(data[0].h36WeatherCode, 0) };
    //threeDay.marker={symbol:getSymbolByWeatherCode(data[0].h60WeatherCode, 0) };
    oneDay.marker={symbol: 'url(../img/dayimg/'+h24WeatherCode+'.png)'};
    twoDay.marker={symbol: 'url(../img/dayimg/'+h48WeatherCode+'.png)' };
    threeDay.marker={symbol:'url(../img/dayimg/'+h72WeatherCode+'.png)' };
    fourDay.marker={symbol:'url(../img/dayimg/'+h96WeatherCode+'.png)' };
    fiveDay.marker={symbol:'url(../img/dayimg/'+h120WeatherCode+'.png)' };
    sixDay.marker={symbol:'url(../img/dayimg/'+h144WeatherCode+'.png)' };
    sevenDay.marker={symbol:'url(../img/dayimg/'+h168WeatherCode+'.png)' };
    //oneNight.marker={symbol:getSymbolByWeatherCode(data[0].h24WeatherCode, 1) };
    //twoNight.marker={symbol:getSymbolByWeatherCode(data[0].h48WeatherCode, 1) };
    //threeNight.marker = { symbol: getSymbolByWeatherCode(data[0].h72WeatherCode, 1) };
    oneNight.marker={symbol:'url(../img/nightimg/n'+h24WeatherCode+'.png)' };
    twoNight.marker={symbol:'url(../img/nightimg/n'+h48WeatherCode+'.png)' };
    threeNight.marker = { symbol: 'url(../img/nightimg/n'+h72WeatherCode+'.png)' };
    fourNight.marker = { symbol: 'url(../img/nightimg/n'+h96WeatherCode+'.png)' };
    fiveNight.marker = { symbol: 'url(../img/nightimg/n'+h120WeatherCode+'.png)' };
    sixNight.marker = { symbol: 'url(../img/nightimg/n'+h144WeatherCode+'.png)' };
    sevenNight.marker = { symbol: 'url(../img/nightimg/n'+h168WeatherCode+'.png)' };

    dayData = [oneDay, twoDay, threeDay, fourDay, fiveDay, sixDay, sevenDay];
    nightData = [oneNight, twoNight, threeNight, fourNight, fiveNight, sixNight, sevenNight];
    //var chart = $('#container').highcharts();
    //chart.series[0].setData(dayData, true);
    //chart.series[1].setData(nightData, true);
    //chart.redraw();

    //initChart(dayData,nightData);
    initChart(dayData,nightData);
}

function initChart(dayData,nightData) {
    $('#container')
        .highcharts(
            {
                chart: {
                    type: 'spline' //设置折线平滑
                },
                credits: {
                    enabled: false
                },
                title: {
                    text: title,
                    style: {
                        color: '#fff'
                    }
                },
                xAxis: {
                    tickInterval: 24 * 3600 * 1000,
                    type: 'datetime',
                    labels: {
                        style: {
                            color:'#fff',//坐标轴刻度颜色
                            fontSize:12,
                            fontWeight:'bolder'},
                        format: '{value:%m月%e日}'
                    }
                },
                yAxis: {
                    labels: {
                        style: {
                            color:'#fff',//坐标轴刻度颜色
                            fontSize:12,
                            fontWeight:'bolder'},
                        formatter: function () {
                            return this.value
                        }
                    },
                    title: {
                        enabled: false,
                        text: '温度 (°C)'
                    }
                },
                /*  tooltip: {
                 crosshairs: true,
                 shared: true
                 }, */
                tooltip: {
                    enabled: false,
                    crosshairs: true,
                    shared: true,
                    useHTML: true,
                    formatter: function () {
                        return tooltipFormatter(this);
                    }
                },
                plotOptions: {
                    series: {
                        dataLabels: {
                            enabled: true,
                            y: -12,
                            format: '{y} °C'
                        }
                    }
                },
                legend: {
                    enabled: false
                },
                series: [
                    {
                        name: '高温',
                        color:'#C95843',  //设置曲线颜色
                        marker: {
                            symbol: 'square',
                        },
                        tooltip: {
                            valueSuffix: ' °C'
                        },
                        //pointStart: Date.UTC(2014, 11, 15),
                        //pointInterval: 24 * 3600 * 1000,
                        data:dayData
                    },
                    {
                        name: '低温',
                        marker: {
                            symbol: 'diamond'
                        },
                        tooltip: {
                            valueSuffix: ' °C'
                        },
                        data: nightData
                    }]
            });
}

function getSymbolByWeatherCode(weatherCode,dayOrNight) {
    var url = "../img/weatherIcon/wp-" + weatherCode + ".png";
    return url;
}

//日期转换
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

